package com.element.security.server.web;

import com.common.core.results.SingleResult;
import com.element.security.model.co.TokenRefreshCO;
import com.element.security.server.global.GlobalOAuth2RequestFactory;
import com.element.security.server.service.IAuthCodeTokenService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @auther WuJun.Zhang
 * @date 2022/2/18 下午10:09
 */
@Controller
@RequestMapping(value = "oauth")
public class AuthCodeTokenController {

    @Resource(name = "authCodeTokenService")
    private IAuthCodeTokenService authCodeTokenService;

    @Resource
    private GlobalOAuth2RequestFactory globalOAuth2RequestFactory;

    @ResponseBody
    @GetMapping(value = "code")
    public SingleResult<TokenRefreshCO> getCode(@RequestParam Map<String, String> parameters) {
        authCodeTokenService.setOAuth2RequestFactory(globalOAuth2RequestFactory);
        return authCodeTokenService.code(parameters);
    }

    @ResponseBody
    @PostMapping(value = "code")
    public SingleResult<TokenRefreshCO> postCode(@RequestParam Map<String, String> parameters) {
        return authCodeTokenService.code(parameters);
    }
}